package team.se.tms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.se.tms.dao.impl.DaoImpl;
import team.se.tms.dto.Page;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.po.LabelInfo;
import team.se.tms.service.LabelInfoService;
import team.se.tms.util.KeyUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标签业务层
 *
 * @author johnny
 * @date 2018/11/24
 */
@Service("LabelInfoService")
@Transactional(rollbackFor = Exception.class)
public class LabelInfoServiceImpl  implements LabelInfoService{

    @Autowired
    public DaoImpl dao;

    /**
     * 按分页查询标签
     *
     * @param page 分页
     * @return List<LabelInfo> 标签列表
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public List<LabelInfo> listAllLabelInfos(Page page) throws Exception{
        return dao.selectList("LabelInfoMapper.selectAllLabelInfos",page);
    }

    /**
     * 添加标签
     *
     * @param labelInfo 标签信息
     * @return Boolean 添加结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public Boolean saveLabelInfo(LabelInfo labelInfo) throws Exception{
        labelInfo.setLabelId(KeyUtil.generatePrimaryKey());
        String labelName = labelInfo.getLabelName();
        if((int)dao.selectOne("LabelInfoMapper.selectLabelInfoCountByLabelName",labelName)>0){
            throw new InsertException("标签已存在");
        }
        if(dao.update("LabelInfoMapper.insertLabelInfo",labelInfo)>0){
            return true;
        }
        return false;
    }

    /**
     * 修改标签
     *
     * @param labelInfo 标签信息
     * @return Boolean 修改结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public Boolean modifyLabelInfo(LabelInfo labelInfo) throws Exception{
        //判断该标签是否存在
        if((int)dao.selectOne("LabelInfoMapper.selectLabelInfoCountByLabelName",labelInfo.getLabelName())>0){
            throw new InsertException("标签已存在");
        }
        //更新标签信息
        if(dao.update("LabelInfoMapper.updateLabelInfo",labelInfo)>0){
            return true;
        }
        return false;
    }

    /**
     * 删除标签
     *
     * @param labelId 标签id
     * @return Boolean 删除结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public Boolean removeLabelInfo(String labelId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("labelId",labelId);
        //存储过程的输出参数
        map.put("numInLabelLib",-1);
        int result =dao.update("LabelInfoMapper.deleteLabelInfoByLabelId",map);
        if((int)map.get("numInLabelLib")>0){
            //若在作业中被使用
            throw new DeleteException("标签被使用，无法删除");
        }else if(result>0){
            return true;
        }
        return false;
    }

}
